package ro.fortech.peaa.service.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ro.fortech.peaa.domain.Employee;
import ro.fortech.peaa.repository.api.IEmployeeMapper;
import ro.fortech.peaa.service.api.IEmployeeManager;

public class EmployeeManager implements IEmployeeManager {
	
	private IEmployeeMapper employeeMapper;
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void setEmployeeMapper(IEmployeeMapper employeeMapper) {
		this.employeeMapper = employeeMapper;
	}

	@Override
	public void deleteEmployee(Employee employee) throws Exception {
		employeeMapper.delete(employee);
	}

	@Override
	public Employee find(Integer id) throws Exception {
		return employeeMapper.find(id);
	}

	@Override
	public List<Employee> findAll() throws Exception {
		return employeeMapper.findAll();
	}

	@Override
	public void insertEmployee(Employee employee) throws Exception {
		employeeMapper.insert(employee);
	}

	@Override
	public void updateEmployee(Employee employee) throws Exception {
		employeeMapper.update(employee);
	}

	@Override
	public Double getAverageSalary() throws Exception {
		List<Employee> employees = employeeMapper.findAll();
		Double sum = new Double(0);
		for (Iterator<Employee> it = employees.iterator(); it.hasNext();) {
			sum += ((Employee) it.next()).getSalary();
		}
		return sum / employees.size();
	}

}
